package guoanmaker.operator.business.service;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import guoanmaker.operator.business.model.OperatorBoutiqueRecommend;
import guoanmaker.operator.business.model.OutOperatorBoutiqueRecommend;
import guoanmaker.operator.business.model.ProvideCommodity;
import guoanmaker.operator.business.model.ProvideCommodityServiceRange;
import guoanmaker.operator.business.model.ProvideRoom;
import guoanmaker.operator.business.modelnobind.ProvideCommodityMove;
import guoanmaker.operator.business.modelnobind.ProvideCommodityServiceMove;
import guoanmaker.operator.business.repository.OperatorBoutiqueRecommendRepository;
import guoanmaker.operator.business.repository.OperatorRoomRepository;
import guoanmaker.operator.business.repository.ProvideCommodityRepository;
import guoanmaker.operator.business.repository.ProvideCommodityServiceRangeRepository;
import guoanmaker.operator.helper.common.AjaxJson;
import guoanmaker.operator.helper.common.util.JsonUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


@Service
public class OperatorBoutiqueRecommendService implements OperatorBoutiqueRecommendServiceInterface {
	@Autowired
	private OperatorBoutiqueRecommendRepository operatorBoutiqueRecommendRepository;
	@Autowired
	private AjaxJson ajaxJson;
	@Autowired
	private OperatorRoomRepository operatorRoomRepository;
	@PersistenceContext
	private EntityManager manager;
	@Autowired
	private ProvideCommodityRepository provideCommodityRepository;
	@Autowired
	private ProvideCommodityServiceRangeRepository provideCommodityServiceRangeRepository;
	@Override
	@Transactional
	public OperatorBoutiqueRecommend saveOperatorBoutiqueRecommend(OperatorBoutiqueRecommend operatorBoutiqueRecommend) {
		// TODO Auto-generated method stub
		return operatorBoutiqueRecommendRepository.save(operatorBoutiqueRecommend);
	}
	@Override
	public List<OutOperatorBoutiqueRecommend> selectOperatorBoutiqueRecommends(String cityCode,Integer page, Integer size) {
		// TODO Auto-generated method stub
		Pageable pageable = new PageRequest(page, size);
		Page<OperatorBoutiqueRecommend> ofls=operatorBoutiqueRecommendRepository.findByCityCode(cityCode,pageable);
	        List<OutOperatorBoutiqueRecommend> outobrs=new ArrayList<OutOperatorBoutiqueRecommend>();
	        
	        for(OperatorBoutiqueRecommend obr:ofls){
	        	OutOperatorBoutiqueRecommend outOperatorBoutiqueRecommend=new OutOperatorBoutiqueRecommend();
	        	try{
	        	if(obr.getProvideCommodityId().contains("S")){
	        		ProvideRoom provideRoom=operatorRoomRepository.findByroomCodes(obr.getProvideCommodityId());
	        		if(provideRoom!=null){
					outOperatorBoutiqueRecommend.setCommitName(provideRoom.getRoomName());
					outOperatorBoutiqueRecommend.setCommitid(provideRoom.getRoomCodes());
					outOperatorBoutiqueRecommend.setCommitUrl(provideRoom.getRoomlogo());
					outOperatorBoutiqueRecommend.setSynopsis(provideRoom.getDescribeAsk());
					outOperatorBoutiqueRecommend.setServerType(obr.getServerType());
					outOperatorBoutiqueRecommend.setCommodityId(provideRoom.getId());
					outobrs.add(outOperatorBoutiqueRecommend);
					}
	        	}else{
	        		ProvideCommodity provideCommodity=provideCommodityRepository.selectByCommodityForCodebq(Integer.parseInt(obr.getProvideCommodityId()));
	        		if(provideCommodity!=null){
	        		Double finalPrice=0.0;
	        		if(provideCommodity.getIsTerritoryRestriction().equals("2")){
	        			finalPrice=provideCommodity.getFinalPrice();
	        		}else if(provideCommodity.getIsTerritoryRestriction().equals("1")){
	        			String jpql2="select new guoanmaker.operator.business.modelnobind.ProvideCommodityServiceMove(s.id,ct.code,a.name,ct.name,p.name,ct.provincecode,s.isPromotion,s.commodityPrice,s.commodityPricePromotion,s.startTime,s.endTime,s.finalPrice)"
	        					+ " from ProvideCommodityServiceRange s left join  s.commodity c left join s.city ct left join s.area a left join s.province p  where  c.commodityCode=:commodityCode and ct.code=:code order by s.finalPrice";
	        			Query query2=manager.createQuery(jpql2);//查询商品提供的服务区域
	        			query2.setParameter("commodityCode", Integer.parseInt(obr.getProvideCommodityId()));
	        			query2.setParameter("code", cityCode);
	        			List<ProvideCommodityServiceMove> lists=query2.getResultList();
	        			
	        			if(lists.size()>0){
	        				finalPrice=lists.get(0).getFinalPrice();
	        			}
	        		}
	        		
	        		
	        		System.out.println(provideCommodity.getCommodityCode()+"商品id");
	        		outOperatorBoutiqueRecommend.setCommitFinnalPrice(finalPrice);
					outOperatorBoutiqueRecommend.setCommitName(provideCommodity.getCommodityName());
					outOperatorBoutiqueRecommend.setCommitid(provideCommodity.getCommodityCode()+"");
					outOperatorBoutiqueRecommend.setCommitUrl(provideCommodity.getCommodity_smallimage());
					outOperatorBoutiqueRecommend.setSynopsis(provideCommodity.getSynopsis());
					outOperatorBoutiqueRecommend.setServerType(Integer.parseInt(provideCommodity.getType()));
					outOperatorBoutiqueRecommend.setCommodityId(provideCommodity.getId());
					outOperatorBoutiqueRecommend.setIsBargaining(provideCommodity.getIsBargaining());
	        	
	        	outobrs.add(outOperatorBoutiqueRecommend);
	        		}
	        	}
	        	}catch(Exception e){
	        		e.printStackTrace();
	        		
	        	}	
			}
		
		return outobrs;
	}
	@Override
	public List<OperatorBoutiqueRecommend> selectOperatorBoutiqueRecommendsPc(String cityCode) {
		// TODO Auto-generated method stub
		return operatorBoutiqueRecommendRepository.findByCityCodePc(cityCode);
	}
	@Override
	public ProvideCommodity selectProvideCommodity(Integer commodityCode) {
		
		// TODO Auto-generated method stub
		return provideCommodityRepository.selectByCommodityBypraThree(commodityCode);
	}
	@Override
	public List<ProvideCommodityServiceRange> selectProvideCommodityServiceRange(String id, String cityId) {
		// TODO Auto-generated method stub
		return provideCommodityServiceRangeRepository.selectBycityandid(id, cityId);
	}
	@Override
	@Transactional
	public void deleteOutOperatorBoutiqueRecommend(String id) {
		// TODO Auto-generated method stub
		operatorBoutiqueRecommendRepository.delete(id);
	}
	
	 @Transactional
	  public void delectAllBycitycode(String cityCode){
		  String jpql="delete  from OperatorBoutiqueRecommend  u where u.cityCode=:cityCode";
		  Query query2=manager.createQuery(jpql);//查询商品提供的服务区域
			query2.setParameter("cityCode", cityCode);
			query2.executeUpdate();
		  
	  }

}
